﻿Public Class Service

    Private _key As String

    Private Const BaseUrl = "https://www.googleapis.com/language/translate/v2"
    Private Const keyParamName = "key"

    Sub New(ByVal key As String)
        _key = key
    End Sub

    Public Function Translate(ByVal sources As System.Collections.Generic.IEnumerable(Of String)) As String
        Dim ub = New System.Text.StringBuilder(BaseUrl)

        Dim parameters = New System.Collections.Generic.List(Of String)

        parameters.Add(String.Format("{0}={1}", keyParamName, _key))
        parameters.Add(String.Format("{0}={1}", "target", "en"))
        For Each source In sources
            parameters.Add(String.Format("{0}={1}", "q", source))
        Next

        ub.AppendFormat("?{0}", String.Join("&", parameters))

        Dim r = System.Net.HttpWebRequest.Create(ub.ToString)

        Dim sr As New System.IO.StreamReader(r.GetResponse.GetResponseStream)

        Return sr.ReadToEnd
    End Function
End Class
